/**
 * <p>Copyright: Copyright (c) 2016</p>
 * <p>Company: gzhc365 </p>
 */
package cn.midi365.train.scheduler;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import cn.midi365.train.cache.TokenCache;
import cn.midi365.train.common.ErrorCode;
import cn.midi365.train.comstans.TrainConstants;
import cn.midi365.train.service.AppService;
import cn.midi365.train.vo.AppDto;

import com.alibaba.fastjson.JSONObject;
import com.eteam.common.entity.TeamContext;
import com.eteam.common.helper.TeamContextHelper;
import com.eteam.common.utils.HttpClientUtils;
import com.eteam.common.utils.Utils;
import com.eteam.exceptions.BizException;

import freemarker.template.utility.StringUtil;

/**
 *
 * @author zihong
 * @date: 2017年12月4日 上午11:26:10
 * @version 1.0
 * @since JDK 1.7
 */
@Service("wxAppScheduler")
public class WxAppScheduler {
    Logger logger = LoggerFactory.getLogger(WxAppScheduler.class);

    /**
     * 提醒测量 每周二、四 01 点执行
     * 
     * @author zihong
     * @date: 2017年12月4日 上午11:32:49
     * @version 1.0
     *
     */
    @Resource(name = "teamContextHelper")
    private TeamContextHelper teamContextHelper;

    private AppService miniProgramService;

    @Autowired
    TokenCache tokenCache;

    @Scheduled(cron = "0 0 0/1 * * *")
    public void getAccessToken() {

        TeamContext context = teamContextHelper.generatorHcContext();
        List<AppDto> miniPrograms = miniProgramService.getMiniPrograms(context);

        for (AppDto miniProgram : miniPrograms) {
            String token = miniProgramService.getAccesToken(miniProgram.getAppId(), context);
            if (!StringUtils.isBlank(token)) {
                tokenCache.set(TrainConstants.access_token + miniProgram.getAppId(), token);
            }
        }

    }

}
